package bookmanage;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MemberInsert extends JInternalFrame {
	private JLabel lblId;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField userid;
	private JButton btnNewButton;
	private JPasswordField userpwd1;
	private JPasswordField userpwd2;
	private JTextField username;
	private JLabel label_3;
	private JRadioButton radio1;
	private JRadioButton radio2;
	private JLabel label_4;
	private JComboBox birth_year;
	private JComboBox birth_month;
	private JComboBox birth_day;
	private JLabel label_5;
	JTextField zipcode;
	private JButton btnNewButton_1;
	private JLabel label_6;
	JTextField addr1;
	private JTextField addr2;
	private JLabel label_7;
	private JTextField email1;
	private JComboBox email2;
	private JLabel label_8;
	private JComboBox phone1;
	private JLabel lblNewLabel;
	private JLabel label_9;
	private JTextField phone2;
	private JLabel label_10;
	private JTextField phone3;
	private JLabel label_11;
	private JComboBox grade;
	private JLabel label_12;
	private JComboBox classname;
	private JButton btnNewButton_2;
	private JLabel sajin;
	private JButton button;

	boolean idFlag=false;
	boolean addrFlag=false;
	Connection con;
	String fileName="";
	String ok_id="";
	
	public MemberInsert(BMMain main) {
		super("회원 가입",true,true,false,true);
		this.con=main.con;
		getContentPane().setLayout(null);
		getContentPane().add(getLblId());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_1_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getUserid());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getUserpwd1());
		getContentPane().add(getUserpwd2());
		getContentPane().add(getUsername());
		getContentPane().add(getLabel_3());
		getContentPane().add(getRadio1());
		getContentPane().add(getRadio2());
		getContentPane().add(getLabel_4());
		getContentPane().add(getBirth_year());
		getContentPane().add(getBirth_month());
		getContentPane().add(getBirth_day());
		getContentPane().add(getLabel_5());
		getContentPane().add(getTextField_2());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLabel_6());
		getContentPane().add(getTextField_1_1());
		getContentPane().add(getTextField_2_1());
		getContentPane().add(getLabel_7());
		getContentPane().add(getEmail1());
		getContentPane().add(getComboBox_1());
		getContentPane().add(getLabel_8());
		getContentPane().add(getComboBox_1_1());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel_9());
		getContentPane().add(getPhone2());
		getContentPane().add(getLabel_10());
		getContentPane().add(getPhone3());
		getContentPane().add(getLabel_11());
		getContentPane().add(getGrade());
		getContentPane().add(getLabel_12());
		getContentPane().add(getClassname());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getSajin());
		getContentPane().add(getButton());
		ButtonGroup bg = new ButtonGroup();
		bg.add(radio1);
		bg.add(radio2);
		radio1.setSelected(true);
		for(int i=1;i<=31;i++){
			if(i<10) birth_day.addItem("0"+i+"일");
			else birth_day.addItem(i+"일");
		}
		setResizable(false);
		selectClass();
	}
	private JLabel getLblId() {
		if (lblId == null) {
			lblId = new JLabel("아이디");
			lblId.setFont(new Font("굴림", Font.BOLD, 14));
			lblId.setBounds(10, 10, 70, 25);
		}
		return lblId;
	}
	private JLabel getLabel_1() {
		if (label == null) {
			label = new JLabel("이 름");
			label.setFont(new Font("굴림", Font.BOLD, 14));
			label.setBounds(10, 115, 70, 25);
		}
		return label;
	}
	private JLabel getLabel_1_1() {
		if (label_1 == null) {
			label_1 = new JLabel("비밀번호");
			label_1.setFont(new Font("굴림", Font.BOLD, 14));
			label_1.setBounds(10, 45, 70, 25);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("재확인");
			label_2.setFont(new Font("굴림", Font.BOLD, 14));
			label_2.setBounds(10, 80, 85, 25);
		}
		return label_2;
	}
	private JTextField getUserid() {
		if (userid == null) {
			userid = new JTextField();
			userid.setBounds(92, 13, 125, 24);
			userid.setColumns(10);
		}
		return userid;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("중복 체크");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String m_id = userid.getText().trim();
					if(m_id.equals("")){
						JOptionPane.showMessageDialog(null, "아이디를 입력해주세요!");
						return;
					}
					
					String sql="select count(*) from membertbl where m_id=?";
					try {
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, m_id);
						ResultSet rs = psmt.executeQuery();
						if(rs.next()){
							int cnt = rs.getInt(1);
							if(cnt==1){
								idFlag=false;
								JOptionPane.showMessageDialog(null, m_id+"는 이미 사용중입니다!");
							}else{
								idFlag=true;
								JOptionPane.showMessageDialog(null, "사용 가능한 아이디 입니다~");
								ok_id=m_id;
							}
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					
					
				}
			});
			btnNewButton.setBounds(228, 12, 85, 35);
		}
		return btnNewButton;
	}
	private JPasswordField getUserpwd1() {
		if (userpwd1 == null) {
			userpwd1 = new JPasswordField();
			userpwd1.setBounds(92, 48, 125, 24);
		}
		return userpwd1;
	}
	private JPasswordField getUserpwd2() {
		if (userpwd2 == null) {
			userpwd2 = new JPasswordField();
			userpwd2.setBounds(92, 83, 125, 24);
		}
		return userpwd2;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setColumns(10);
			username.setBounds(92, 115, 125, 24);
		}
		return username;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("성 별");
			label_3.setFont(new Font("굴림", Font.BOLD, 14));
			label_3.setBounds(10, 145, 70, 25);
		}
		return label_3;
	}
	private JRadioButton getRadio1() {
		if (radio1 == null) {
			radio1 = new JRadioButton("여자");
			radio1.setBounds(92, 145, 60, 25);
		}
		return radio1;
	}
	private JRadioButton getRadio2() {
		if (radio2 == null) {
			radio2 = new JRadioButton("남자");
			radio2.setBounds(156, 145, 60, 25);
		}
		return radio2;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("생 일");
			label_4.setFont(new Font("굴림", Font.BOLD, 14));
			label_4.setBounds(10, 180, 70, 25);
		}
		return label_4;
	}
	private JComboBox getBirth_year() {
		if (birth_year == null) {
			birth_year = new JComboBox();
			for(int i=2014;i>=1910;i--){
				birth_year.addItem(i+"년");
			}
			birth_year.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String yearS = (String)birth_year.getSelectedItem();
					String monthS = (String)birth_month.getSelectedItem();
					monthS = monthS.substring(0, 2);
					if(monthS.equals("01") || monthS.equals("03") || monthS.equals("05") || monthS.equals("07")
							|| monthS.equals("08") || monthS.equals("10") || monthS.equals("12")){
						for(int i=1;i<=31;i++){
							if(i<10) birth_day.addItem("0"+i+"일");
							else birth_day.addItem(i+"일");
						}
					}else if(monthS.equals("04") || monthS.equals("06") || monthS.equals("09") || monthS.equals("11")){
						for(int i=1;i<=30;i++){
							if(i<10) birth_day.addItem("0"+i+"일");
							else birth_day.addItem(i+"일");
						}
					}else{
						int yearI = Integer.parseInt(yearS.substring(0, 4));
						if(yearI%4==0){
							for(int i=1;i<=29;i++){
								if(i<10) birth_day.addItem("0"+i+"일");
								else birth_day.addItem(i+"일");
							}
						}else{
							for(int i=1;i<=28;i++){
								if(i<10) birth_day.addItem("0"+i+"일");
								else birth_day.addItem(i+"일");
							}
						}
					}
				}
			});
			birth_year.setBounds(92, 180, 125, 24);
		}
		return birth_year;
	}
	private JComboBox getBirth_month() {
		if (birth_month == null) {
			birth_month = new JComboBox();
			for(int i=1;i<=12;i++){
				if(i<10) birth_month.addItem("0"+i+"월");
				else birth_month.addItem(i+"월");
			}
			birth_month.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String yearS = (String)birth_year.getSelectedItem();
					String monthS = (String)birth_month.getSelectedItem();
					monthS = monthS.substring(0, 2);
					if(monthS.equals("01") || monthS.equals("03") || monthS.equals("05") || monthS.equals("07")
							|| monthS.equals("08") || monthS.equals("10") || monthS.equals("12")){
						for(int i=1;i<=31;i++){
							if(i<10) birth_day.addItem("0"+i+"일");
							else birth_day.addItem(i+"일");
						}
					}else if(monthS.equals("04") || monthS.equals("06") || monthS.equals("09") || monthS.equals("11")){
						for(int i=1;i<=30;i++){
							if(i<10) birth_day.addItem("0"+i+"일");
							else birth_day.addItem(i+"일");
						}
					}else{
						int yearI = Integer.parseInt(yearS.substring(0, 4));
						if(yearI%4==0){
							for(int i=1;i<=29;i++){
								if(i<10) birth_day.addItem("0"+i+"일");
								else birth_day.addItem(i+"일");
							}
						}else{
							for(int i=1;i<=28;i++){
								if(i<10) birth_day.addItem("0"+i+"일");
								else birth_day.addItem(i+"일");
							}
						}
					}
				}
			});
			birth_month.setBounds(238, 180, 70, 24);
		}
		return birth_month;
	}
	private JComboBox getBirth_day() {
		if (birth_day == null) {
			birth_day = new JComboBox();
			birth_day.setBounds(331, 180, 70, 24);
		}
		return birth_day;
	}
	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("우편번호");
			label_5.setFont(new Font("굴림", Font.BOLD, 14));
			label_5.setBounds(10, 215, 70, 25);
		}
		return label_5;
	}
	private JTextField getTextField_2() {
		if (zipcode == null) {
			zipcode = new JTextField();
			zipcode.setEditable(false);
			zipcode.setBounds(92, 215, 125, 24);
			zipcode.setColumns(10);
		}
		return zipcode;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(addrFlag==false){
						addrFlag=true;
						Address addr = new Address(MemberInsert.this);
						addr.setVisible(true);
						addr.setAlwaysOnTop(true);
						
					}
				}
			});
			btnNewButton_1.setBounds(238, 215, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("주소");
			label_6.setFont(new Font("굴림", Font.BOLD, 14));
			label_6.setBounds(10, 250, 70, 25);
		}
		return label_6;
	}
	private JTextField getTextField_1_1() {
		if (addr1 == null) {
			addr1 = new JTextField();
			addr1.setEditable(false);
			addr1.setBounds(92, 250, 330, 24);
			addr1.setColumns(10);
		}
		return addr1;
	}
	private JTextField getTextField_2_1() {
		if (addr2 == null) {
			addr2 = new JTextField();
			addr2.setColumns(10);
			addr2.setBounds(92, 285, 330, 24);
		}
		return addr2;
	}
	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("이메일");
			label_7.setFont(new Font("굴림", Font.BOLD, 14));
			label_7.setBounds(10, 320, 70, 25);
		}
		return label_7;
	}
	private JTextField getEmail1() {
		if (email1 == null) {
			email1 = new JTextField();
			email1.setBounds(92, 320, 125, 24);
			email1.setColumns(10);
		}
		return email1;
	}
	private JComboBox getComboBox_1() {
		if (email2 == null) {
			email2 = new JComboBox();
			email2.setOpaque(false);
			email2.setBackground(Color.WHITE);
			email2.setBounds(248, 320, 140, 24);
			email2.addItem("nate.com");
			email2.addItem("cyworld.com");
			email2.addItem("empas.com");
			email2.addItem("lycos.co.kr");
			email2.addItem("netsgo.com");
			email2.addItem("hanmail.net");
			email2.addItem("naver.com");
			email2.addItem("yachoo.co.kr");
			email2.addItem("paran.com");
			email2.addItem("hotmail.com");
			email2.addItem("dreamwiz.com");
			email2.addItem("gmail.com");
		}
		return email2;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("폰번호");
			label_8.setFont(new Font("굴림", Font.BOLD, 14));
			label_8.setBounds(10, 355, 70, 25);
		}
		return label_8;
	}
	private JComboBox getComboBox_1_1() {
		if (phone1 == null) {
			phone1 = new JComboBox();
			phone1.setBounds(92, 355, 85, 24);
			phone1.addItem("010");
			phone1.addItem("011");
			phone1.addItem("015");
			phone1.addItem("016");
			phone1.addItem("017");
			phone1.addItem("018");
			phone1.addItem("019");
		}
		return phone1;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("@");
			lblNewLabel.setBounds(228, 320, 19, 22);
		}
		return lblNewLabel;
	}
	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("-");
			label_9.setBounds(189, 355, 19, 22);
		}
		return label_9;
	}
	private JTextField getPhone2() {
		if (phone2 == null) {
			phone2 = new JTextField();
			phone2.setBounds(199, 355, 70, 24);
			phone2.setColumns(10);
		}
		return phone2;
	}
	private JLabel getLabel_10() {
		if (label_10 == null) {
			label_10 = new JLabel("-");
			label_10.setBounds(275, 355, 19, 22);
		}
		return label_10;
	}
	private JTextField getPhone3() {
		if (phone3 == null) {
			phone3 = new JTextField();
			phone3.setColumns(10);
			phone3.setBounds(287, 355, 70, 24);
		}
		return phone3;
	}
	private JLabel getLabel_11() {
		if (label_11 == null) {
			label_11 = new JLabel("회원 등급");
			label_11.setFont(new Font("굴림", Font.BOLD, 14));
			label_11.setBounds(10, 425, 70, 25);
		}
		return label_11;
	}
	private JComboBox getGrade() {
		if (grade == null) {
			grade = new JComboBox();
			grade.setBounds(92, 425, 125, 24);
			grade.addItem("메니져");
			grade.addItem("강사");
			grade.addItem("수강생");
		}
		return grade;
	}
	private JLabel getLabel_12() {
		if (label_12 == null) {
			label_12 = new JLabel("강좌명");
			label_12.setFont(new Font("굴림", Font.BOLD, 14));
			label_12.setBounds(10, 390, 70, 25);
		}
		return label_12;
	}
	private JComboBox getClassname() {
		if (classname == null) {
			classname = new JComboBox();
			classname.addItem("선택");
			classname.setBounds(92, 390, 125, 24);
		}
		return classname;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("회원 등록");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String m_id = userid.getText().trim();
					String m_name = username.getText().trim();
					String pwd1 = userpwd1.getText().trim();
					String pwd2 = userpwd2.getText().trim();
					String gender="";
					if(radio1.isSelected()==true) gender="여자";
					else if(radio2.isSelected()==true) gender="남자";
					String birth=""; 
					String b_y=(String)birth_year.getItemAt(birth_year.getSelectedIndex());
					String b_m=(String)birth_month.getItemAt(birth_month.getSelectedIndex());
					String b_d=(String)birth_day.getItemAt(birth_day.getSelectedIndex());
					birth = b_y.substring(2, 4)+"/"+b_m.substring(0, 2)+"/"+b_d.substring(0, 2);
					String z_code=zipcode.getText().trim();
					String addr = addr1.getText().trim()+"/"+addr2.getText().trim();
					String email = email1.getText().trim()+"@"+email2.getItemAt(email2.getSelectedIndex());
					String ph1 = (String)phone1.getItemAt(phone1.getSelectedIndex());
					String phone = ph1+"-"+phone2.getText().trim()+"-"+phone3.getText().trim();
					String gra = (String)grade.getItemAt(grade.getSelectedIndex());
					String c_name = (String)classname.getItemAt(classname.getSelectedIndex());
					
					if(m_id.equals("")){
						JOptionPane.showMessageDialog(null, "아이디를 입력해주세요!");
						userid.requestFocus();
						return;
					}
					if(idFlag==false || !ok_id.equals(m_id)){
						JOptionPane.showMessageDialog(null, "아이디 중복체크 해주세요!");
						userid.requestFocus();
						return;
					}
					if(pwd1.equals("")){
						JOptionPane.showMessageDialog(null, "비밀번호를 입력해주세요!");
						userpwd1.requestFocus();
						return;
					}
					if(pwd2.equals("")){
						JOptionPane.showMessageDialog(null, "재확인을 입력해주세요!");
						userpwd2.requestFocus();
						return;
					}
					if(!pwd1.equals(pwd2)){
						JOptionPane.showMessageDialog(null, "비밀번호가 서로 다릅니다!");
						userpwd1.requestFocus();
						return;
					}
					if(m_name.equals("")){
						JOptionPane.showMessageDialog(null, "이름을 입력해주세요!");
						username.requestFocus();
						return;
					}
					if(z_code.equals("")){
						JOptionPane.showMessageDialog(null, "우편번호를 입력해주세요!");
						return;
					}
					if(addr.equals("")){
						JOptionPane.showMessageDialog(null, "주소를 입력해주세요!");
						addr2.requestFocus();
						return;
					}
					if(email1.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "이메일을 입력해주세요!");
						email1.requestFocus();
						return;
					}
					if(phone2.getText().trim().length()<3 && phone2.getText().trim().length()>4){
						JOptionPane.showMessageDialog(null, "가운데 자리는 4자리를 입력해주세요!");
						phone2.requestFocus();
						return;
					}
					if(phone3.getText().trim().length()!=4){
						JOptionPane.showMessageDialog(null, "마지막 자리는 4자리를 입력해주세요!");
						phone3.requestFocus();
						return;
					}
					try {
						Integer.parseInt(phone2.getText().trim());
						Integer.parseInt(phone3.getText().trim());
					} catch (Exception e2) {
						JOptionPane.showMessageDialog(null, "핸드폰 번호는 숫자만 입력해주세요!");
						phone2.requestFocus();
						return;
					}
					if(gra.equals("수강생") && c_name.equals("선택")){
						JOptionPane.showMessageDialog(null, "수강생은 클래스명을 선택해주세요!");
						return;
					}
					if(!gra.equals("수강생")) c_name="";
					
					
					try {
						String sql = "insert into membertbl"
								+ "(m_id,classname,m_name,m_pwd,m_gender,m_birth,"
								+ "m_zipcode,m_addr,m_phone,m_email,m_grade,m_photo) "
								+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, m_id);
						psmt.setString(2, c_name);
						psmt.setString(3, m_name);
						psmt.setString(4, pwd1);
						psmt.setString(5, gender);
						psmt.setString(6, birth);
						psmt.setString(7, z_code);
						psmt.setString(8, addr);
						psmt.setString(9, phone);
						psmt.setString(10, email);
						psmt.setString(11, gra);
						psmt.setString(12, fileName);
						
						int cnt = psmt.executeUpdate();
						if(cnt==1) JOptionPane.showMessageDialog(null, "회원 등록 되었습니다!");
						else JOptionPane.showMessageDialog(null, "회원 등록 실패!!!");
						
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btnNewButton_2.setFont(new Font("굴림", Font.BOLD, 15));
			btnNewButton_2.setBounds(361, 460, 100, 30);
		}
		return btnNewButton_2;
	}
	private JLabel getSajin() {
		if (sajin == null) {
			sajin = new JLabel("");
			sajin.setBounds(325, 15, 150, 150);
			sajin.setBorder(new LineBorder(new Color(0, 0, 0)));
		}
		return sajin;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("사진 추가");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser jfc = new JFileChooser();
					int yn = jfc.showOpenDialog(null);
					if(yn==0){
						String filePath = jfc.getSelectedFile().toString();
						fileName = jfc.getSelectedFile().getName();
						if(!fileName.toUpperCase().contains(".PNG") && !fileName.toUpperCase().contains(".JPG")){
							JOptionPane.showMessageDialog(null, "jpg,png 등 이미지 파일만 등록 가능합니다");
							return;
						}
						try {
							FileInputStream fis = new FileInputStream(filePath);
							FileOutputStream fos = new FileOutputStream("mem_img/"+fileName);
							
							int data=0;
							byte[]  b = new byte[2048];
							while((data=fis.read(b))!=-1){
								fos.write(b, 0, data);
								fos.flush();	
							}
							
							fis.close();
							fos.close();
							
							Image img = Toolkit.getDefaultToolkit().getImage("mem_img/"+fileName);
							img = img.getScaledInstance(150, 150, 0);
							ImageIcon icon = new ImageIcon(img);
							sajin.setIcon(icon);
							
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
			});
			button.setBounds(228, 135, 85, 35);
		}
		return button;
	}
	
	public void selectClass(){
		try {
			String sql="select small_name from codetbl where big_name='강좌' and small_name is not null";
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				classname.addItem(rs.getString(1));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
